// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <gtest/gtest.h>
#include <pollux/dwio/dwrf/common/checksum.h>

using namespace ::testing;
using namespace kumo::pollux::dwrf;

class ChecksumTests : public Test {
 protected:
  void SetUp() override {
    for (size_t i = 0; i < data.size(); ++i) {
      data.data()[i] = static_cast<char>(i % 0xff);
    }
  }

  void TearDown() override {}

  void runTest(proto::ChecksumAlgorithm type, int64_t c1, int64_t c2) {
    auto checksum = ChecksumFactory::create(type);
    checksum->update(data.data(), data.size());
    auto v1 = checksum->getDigest();
    checksum->update(data.data(), data.size());
    auto v2 = checksum->getDigest(false);
    ASSERT_EQ(v1, v2);
    ASSERT_EQ(v2, c1);
    checksum->update(data.data(), data.size());
    v2 = checksum->getDigest();
    ASSERT_EQ(v2, c2);
  }

  std::array<char, 4096> data;
};

TEST_F(ChecksumTests, Null) {
  auto checksum = ChecksumFactory::create(proto::ChecksumAlgorithm::NULL_);
  ASSERT_EQ(checksum, nullptr);
}

TEST_F(ChecksumTests, xxHash) {
  runTest(
      proto::ChecksumAlgorithm::XXHASH,
      -5912557168167935695,
      2625948533963027735);
}

TEST_F(ChecksumTests, Crc32) {
  runTest(proto::ChecksumAlgorithm::CRC32, 4133052486, 3074245904);
}
